<?php

class Core_Request_Http_Identity extends Soul_Request_Http_Identity {
	
	protected $data = array('roles'=>array(), 
							'actions'=>array(),
							'accounts'=>array(),
							'login'=>FALSE, 
							'id'=>0,
							'name'=>'');

    public function __construct() {	
        parent::__construct();
    }
    
    public function setName($value) {
    	$this->addAccount($value);
		return parent::setName($value);
    }
    
    public function getIsSuper(){
    	return in_array('superadmin', $this->data['roles']);
    }
    
    public function setId($value){
    	$this->data['id'] = $value;
    	return $this;
    }
    
    public function getId(){
    	return $this->data['id'];
    }

    public function addAccount($account){
		if(is_array($account)){
			$this->data['accounts'] = array_merge($this->data['accounts'], $account);
		}else{
			$this->data['accounts'][] = $account;
		}
		$this->data['accounts'] = array_unique($this->data['accounts']);
		return $this;    	
    }
    
	public function hasAccount($account){
		if($this->getIsSuper()){
			return TRUE;
		}
		return in_array($account, $this->data['accounts']);
	}    
    
    public function getAccounts(){
    	return $this->data['accounts'];
    }
    
	public function addAction($action){
		if(is_array($action)){
			$this->data['actions'] = array_merge($this->data['actions'], $action);
		}else{
			$this->data['actions'][] = $action;
		}
		return $this;
	}

    public function isInRole($code) {
    	if($this->getIsSuper()){
    		return TRUE;
    	}
        return parent::isInRole($code);
    }	
    
    public function getRoleIds(){
    	$result = array();
    	foreach($this->data['roles'] as $role){
    		$result[] = $role['id'];
    	}
    	return $result;
    }
	
	public function isAllowAction($action){
		if($this->getIsSuper()){
			return TRUE;
		}
		return in_array($action, $this->data['actions']);
	}
	
	public function isAllowAnyAction(array $actions){
		foreach($actions as $action){
			if($this->isAllowAction($action)){
				return TRUE;
			}
		}
		
		return FALSE;
	}    
}